package org.example.camundaapp.api;

import com.alibaba.fastjson2.JSONObject;
import org.camunda.bpm.engine.repository.Deployment;
import org.example.camundaapp.bpm.model.DeploymentRequest;
import org.example.camundaapp.bpm.service.CamundaDeployer;
import org.example.camundaapp.bpm.service.JsonToBpmnConverter;
import org.example.camundaapp.bpm.service.JsonToBpmnConverter2;
import org.example.camundaapp.bpm.service.JsonToBpmnConverter3;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 *
 * @className: BpmApi
 * @author: ZHRJ
 * @date: 2025-06-24 14:13
 * @Version: 1.0
 * @description:
 */
@RestController
@RequestMapping("/api/bpm")
public class BpmApi {

    private final static Logger log = LoggerFactory.getLogger(BpmApi.class);



    @RequestMapping("/test")
    public String test(){
        JsonToBpmnConverter2 converter = new JsonToBpmnConverter2();
        String bpmnxml = converter.testCreateProcess();
        log.info("testBpmnXml:{}", bpmnxml);
        DeploymentRequest deploymentRequest = new DeploymentRequest();
        deploymentRequest.setBpmnXml(bpmnxml);
        deploymentRequest.setProcessName("testProcess1");
        Deployment deployment = this.deployProcess(deploymentRequest);
        log.info("deploymentId:{}", deployment.getId());
        return deployment.getId();
    }

    // 传入json创建BPMN模型流程
    @RequestMapping("/createProcessByJson")
    public String createProcessByJson(@RequestBody String bpmnStr) {
        try{
            JsonToBpmnConverter3 jsonToBpmnConverter3 = new JsonToBpmnConverter3();
            JSONObject bpmnJson = JSONObject.parseObject(bpmnStr);
            String bpmn = jsonToBpmnConverter3.jsonToBPMNConvert(bpmnJson);
            log.info("最新转换bpmn: {}", bpmn);

            JSONObject process = bpmnJson.getJSONObject("process");
            String processName = process.getString("name");

            DeploymentRequest deploymentRequest = new DeploymentRequest();
            deploymentRequest.setBpmnXml(bpmn);
            deploymentRequest.setProcessName(processName);
            Deployment deployment = this.deployProcess(deploymentRequest);
            return deployment.getId();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "创建流程失败";
    }
    // 传入json创建BPMN模型流程并生成png流程图
    @RequestMapping("/createProcessByJsonAndPng")
    public String createProcessByJsonAndPng(@RequestBody String bpmnStr) {
        try{
            JsonToBpmnConverter3 jsonToBpmnConverter3 = new JsonToBpmnConverter3();
            JSONObject bpmnJson = JSONObject.parseObject(bpmnStr);
            Map<String,Object> result = jsonToBpmnConverter3.jsonToBPMNConvertAndPng(bpmnJson);
            log.info("最新转换bpmn: {}", result.get("bpmnXml"));

            JSONObject process = bpmnJson.getJSONObject("process");
            String processName = process.getString("name");
            String processKey = process.getString("id");

            DeploymentRequest deploymentRequest = new DeploymentRequest();
            deploymentRequest.setBpmnXml(result.get("bpmnXml").toString());
            deploymentRequest.setPngBytes((byte[]) result.get("pngBytes"));
            deploymentRequest.setProcessName(processName);
            deploymentRequest.setProcessKey(processKey);
            Deployment deployment = this.deployProcess(deploymentRequest);
            return deployment.getId();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "创建流程失败";
    }


    // 创建流程并部署
    @RequestMapping("/createAndDeployProcess")
    public String createAndDeployProcess(@RequestBody String json) {
        try{
            String bpmnxml = this.createProcess(json);
            DeploymentRequest deploymentRequest = new DeploymentRequest();
            deploymentRequest.setBpmnXml(bpmnxml);
            deploymentRequest.setProcessName("myProcess01");
            Deployment deployment = this.deployProcess(deploymentRequest);
            return deployment.getId();
        } catch (Exception e){
            e.printStackTrace();
        }
        return "创建部署失败";
    }

    // 传入json创建流程的bpmn文件
    @RequestMapping("/createProcess")
    public String createProcess(@RequestBody String json) {
        try{
            JsonToBpmnConverter jsonToBpmnConverter = new JsonToBpmnConverter();
            JSONObject jsonObject = JSONObject.parseObject(json);
            String bpmn = jsonToBpmnConverter.convert(jsonObject);
            log.info("bpmn: {}", bpmn);
            return bpmn;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    // 通过bpmn文件路径部署流程
    @RequestMapping("/deployProcessByPath")
    public String deployProcess(String bpmnFilePath) {
        try{
            CamundaDeployer camundaDeployer = new CamundaDeployer();
            return camundaDeployer.deployProcess(bpmnFilePath);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "bpmn文件路径方式部署失败";
    }

    // 将bmpn部署到引擎中
    @RequestMapping("/deployProcess")
    public Deployment deployProcess(@RequestBody DeploymentRequest bpmn) {
        try{
            CamundaDeployer camundaDeployer = new CamundaDeployer();
            if(null != bpmn.getPngBytes() && bpmn.getPngBytes().length > 0){
                return camundaDeployer.deployProcess(bpmn.getBpmnXml(), bpmn.getPngBytes(), bpmn.getProcessName(), bpmn.getProcessKey());
            }
            return camundaDeployer.deployProcess(bpmn.getBpmnXml(), bpmn.getProcessName());
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 删除部署定义
     * @param deploymentId 部署id
      */
    @RequestMapping("/deleteDeployment")
    public String deleteDeployment(String deploymentId) {
        try{
            CamundaDeployer camundaDeployer = new CamundaDeployer();
            camundaDeployer.deleteDeployment(deploymentId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "删除成功";
    }

    // 启动流程
    @RequestMapping("/startProcess")
    public String startProcess(String processDefinitionKey) {
        try{
            CamundaDeployer camundaDeployer = new CamundaDeployer();
            camundaDeployer.startProcessInstance(processDefinitionKey);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "启动成功";
    }

}
